{extend name="public:head"}
{block name="main"}

<form class="layui-form" style="margin: 20px">
    <div class="demoTable">
        条件搜索：
        <div class="layui-inline">
            <select name="from" lay-filter="from_name">
                <option value="">推广方</option>
                {volist name="name" id="vo"}
                <option value="{$vo.name}">{$vo.name}</option>
                {/volist}
            </select>
        </div>
        <div class="layui-inline" id="sign">
            <select name="signData" id="signData">
                <option></option>
            </select>
        </div>

        <div class="layui-inline">
            <input class="layui-input" name="address"  autocomplete="off" placeholder="来源地区">
        </div>

        <div class="layui-inline">
            <input class="layui-input" name="ip"  autocomplete="off" placeholder="注册IP">
        </div>

        <div class="layui-input-inline">
            <input name="time" type="text" class="layui-input" id="date" placeholder="输入时间范围">
        </div>

        <div class="layui-inline">
            <select name="type" lay-verify="">
                <option value="">贷款类型</option>
                <option value="d">单期</option>
                <option value="f">分期</option>
            </select>
        </div>

        <div class="layui-inline">
            <button class="layui-btn" lay-filter="submit" lay-submit>搜索</button>
            <button class="layui-btn layui-btn-primary"  lay-filter="submit" lay-submit>重置</button>
        </div>

    </div>
</form>

<table class="layui-hide" id="all" lay-filter="all" lay-data="{id: 'sousuo'}"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="delAll" id="delAll">删除选择的数据</button>
    </div>
</script>

<script type="text/html" id="getType">
    {{#  if(d.type == 'f'){ }} <a class="layui-btn layui-btn-xs layui-btn-normal">分期 </a> {{#  } }}
    {{#  if(d.type == 'd'){ }} <a class="layui-btn layui-btn-xs">单期 </a> {{#  } }}
</script>


<script type="text/html" id="status">
    <input type="checkbox" name="status"  lay-skin="switch" lay-text="开启|禁用" {{#  if(d.status == 1){ }} checked {{#  } }}>
</script>

<script>
    layui.use(['table','layer','form','laydate'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;
        table.render({
            elem: '#all'
            ,url:'/admin/Popularize/getAllPopularizeApi?model_str=Popularize'
            ,toolbar: '#toolbarDemo'
            ,limit: 14
            ,limits: [10, 20, 40, 80, 160, 320, 640, 1280]
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'from', title:'推广方',width: 90}
                ,{field:'sign', title:'标识码',width: 90}
                ,{field:'ip', title:'来源ip'}
                ,{field:'type', title:'贷款类型', toolbar: '#getType',width: 90}
                ,{field:'address', title:'来源地区'}
                ,{field:'add_time', title:'添加时间'}
                ,{field:'uodate_time', title:'最后时间'}
                ,{field:'record_id', title:'访问记录id'}
                ,{field:'ip_count', title:'当天ip次数'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150 , align: 'center'}
            ]]
            ,page: true
        });
        // 表格重载 用于搜索
        form.on('submit(submit)', function(data){
            var field=data.field;
            if($(data.elem).html()=="重置"){
                $("input").val("");
                field="";
                location.reload();
            }
            table.reload('all', {
                url: '/admin/Popularize/findAllPopularizeApi?model_str=Popularize',
                where: {field}
            });
            return false;
        });

        table.on('tool(all)', function(obj){
            var data = obj.data; //获得当前行数据
            var id = obj.data.id
            if(obj.event == 'del'){
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type: "get",
                        dataType: 'json',
                        url: "/admin/Popularize/delOnePopularizeApi?id="+id,
                        error:function(error){
                            layer.msg('删除失败');
                            layer.close(index);
                        },success: function (data) {
                            if(data.code == 200){
                                layer.msg('删除成功');
                                obj.del();
                            }
                        }
                    })
                });
            }
            if(obj.event == 'hide'){
                layer.confirm('隐藏该记录', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.post("{:Url('admin/Popularize/hideOneRegistered')}",{'id': data.id},function(data){
                        console.log(data.code);
                        if(data.code == 300 ||data.code == 500){
                            layer.msg('失败！！！请重试');
                        }
                        if(data.code == 200){
                            layer.msg('成功！！！');
                            // form.render();
                        }
                    })
                });
            }
            if(obj.event == 'notHide'){
                layer.confirm('显示该记录', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.post("{:Url('admin/Popularize/hideOneRegistered')}",{'id': data.id},function(data){
                        console.log(data.code);
                        if(data.code == 300 ||data.code == 500){
                            layer.msg('失败！！！请重试');
                        }
                        if(data.code == 200){
                            layer.msg('成功！！！');
                        }
                    })
                });
            }
        });


        table.on('toolbar(all)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
                // 一件更新选择的状态
                case 'delAll':
                    // 获取所有选中的记录
                    var data = checkStatus.data;
                    // 定义一个空的数组
                    var arr = [];
                    // 组成一个所有id的数组
                    for( let i of data){
                        arr.push(i.id)
                    }
                    var post_data = JSON.stringify(arr)
                    var index = layer.load(2, {time: 360*1000});
                    $.ajax({
                        type: "POST",
                        dataType: 'json',
                        url: "/admin/Popularize/delCheckListApi",
                        data: {'ids':post_data},
                        error:function(error){
                            layer.msg('删除失败');
                            layer.close(index);
                        },success: function (data) {
                            if(data.code == 200){
                                layer.msg('删除成功');
                                layer.close(index);
                                setTimeout(function () {
                                    location.reload();
                                }, 2000)

                            }
                        }
                    })
                    break;
            };
        });

        laydate.render({
            elem: '#date' //指定元素
            ,range: '.'
        });

        // 点击推广方的input 则标识码的input则自动出现推广方所有标识码
        form.on('select(from_name)', function(data){
            var name = data.value
            $.post({
                url:"/admin/Popularize/findSign",
                type:"post",
                dataType:"json",
                data:{'name':name},
                success:function (res) {
                    if(res.code !== 200){
                        layer.msg('获取标识码是失败');
                    }
                    if(res.code == 200){
                        data = res.data
                        for(var key in data){
                            $("#signData").append("<option value='" + data[key] + "'>" + data[key] + "</option>");
                        }
                        form.render('select');
                    }
                },
                error:function (error) {
                    if(error.responseJSON.code == 409){
                        layer.msg('推广方不存在');
                    }
                }
            })
        });
    });
</script>


{/block}